package cn.dm.service.impl;

import cn.dm.client.*;
import cn.dm.common.BaseException;
import cn.dm.common.Constants;
import cn.dm.common.DateUtil;
import cn.dm.common.EmptyUtils;
import cn.dm.dto.Dto;
import cn.dm.dto.DtoUtil;
import cn.dm.exception.ItemErrorCode;
import cn.dm.pojo.*;
import cn.dm.service.LocalItemService;
import cn.dm.vo.ItemDetailVo;
import cn.dm.vo.ItemPriceVo;
import cn.dm.vo.ItemSchedulerVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LocalItemServiceImpl implements LocalItemService {
    @Autowired
    private RestDmItemClient restDmItemClient;
    @Autowired
    private RestDmCinemaClient restDmCinemaClient;
    @Autowired
    private RestDmImageClient restDmImageClient;
    @Autowired
    private RestDmSchedulerClient restDmSchedulerClient;
    @Autowired
    private RestDmSchedulerSeatPriceClient restDmSchedulerSeatPriceClient;
    @Autowired
    private RestDmSchedulerSeatClient restDmSchedulerSeatClient;
    @Override
    public Dto<ItemDetailVo> getItemInfo(Long id) throws Exception {
        //根据商品ID获得详细信息
        DmItem dmItem=restDmItemClient.getDmItemById(id);
        if (EmptyUtils.isEmpty(dmItem)){
            return null;
        }
        List<DmImage> dmImageList=getImageList(dmItem.getId(),Constants.Image.ImageType.carousel,Constants.Image.ImageCategory.item);

        //获取影院信息
        DmCinema dmCinema=restDmCinemaClient.getDmCinemaById(dmItem.getCinemaId());
        //组装返回数据
        ItemDetailVo itemDetailVo=copyData(dmItem,dmCinema,dmImageList);
        return DtoUtil.returnDataSuccess(itemDetailVo);
    }

    @Override
    public Dto<List<ItemSchedulerVo>> getScheduler(Long itemId) throws Exception {
        //根据商品ID获得详细信息
        DmItem dmItem=restDmItemClient.getDmItemById(itemId);
        if (EmptyUtils.isEmpty(dmItem)){
            throw new BaseException(ItemErrorCode.ITEM_NO_DATA);
        }
        //查询对应的排期列表
        Map<String,Object> param=new HashMap<>();
        param.put("itemId",dmItem.getId());
        List<DmScheduler> dmSchedulerList=restDmSchedulerClient.getDmSchedulerListByMap(param);
        if (EmptyUtils.isEmpty(dmSchedulerList)){
            throw new BaseException(ItemErrorCode.ITEM_NO_DATA);
        }
        //组装返回数据
        List<ItemSchedulerVo> itemSchedulerVoList=new ArrayList<>();
        for (int i=0;i<dmSchedulerList.size();i++){
            ItemSchedulerVo itemSchedulerVo=new ItemSchedulerVo();
            BeanUtils.copyProperties(dmItem,itemSchedulerVo);
            BeanUtils.copyProperties(dmSchedulerList.get(i),itemSchedulerVo);
            //更新显示字段-时间字段
            itemSchedulerVo.setStartTime(DateUtil.format(dmSchedulerList.get(i).getStartTime()));
            itemSchedulerVo.setEndTime(DateUtil.format(dmSchedulerList.get(i).getEndTime()));
            itemSchedulerVoList.add(itemSchedulerVo);
        }
        return DtoUtil.returnDataSuccess(itemSchedulerVoList);
    }

    @Override
    public Dto<List<ItemPriceVo>> queryItemPrice(Long id) throws Exception {
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("scheduleId",id);
        List<DmSchedulerSeatPrice> dmSchedulerSeatPriceList=restDmSchedulerSeatPriceClient.getDmSchedulerSeatPriceListByMap(param);
        if (EmptyUtils.isEmpty(dmSchedulerSeatPriceList)){
         throw new BaseException(ItemErrorCode.ITEM_NO_DATA);
        }
        //组装返回显示信息
        List<ItemPriceVo> itemPriceVoList=new ArrayList<ItemPriceVo>();
        for (DmSchedulerSeatPrice dmSchedulerSeatPrice:dmSchedulerSeatPriceList){
            ItemPriceVo itemPriceVo=new ItemPriceVo();
            BeanUtils.copyProperties(dmSchedulerSeatPrice,itemPriceVo);
            //查询所有是有效的并且没有被锁定的座位
            Map<String,Object> lockMap=new HashMap<String,Object>();
            lockMap.put("status",1);
            lockMap.put("scheduleId",itemPriceVo.getScheduleId());
            int num=restDmSchedulerClient.getDmSchedulerCountByMap(lockMap);
            int isHaveSeat=num>0?1:0;
            itemPriceVo.setIsHaveSeat(isHaveSeat);
            itemPriceVoList.add(itemPriceVo);
        }

        return DtoUtil.returnDataSuccess(itemPriceVoList);
    }

    private ItemDetailVo copyData(DmItem dmItem,DmCinema dmCinema,List<DmImage> dmImageList) throws ParseException {
        //数据封装
        ItemDetailVo itemDetailVo=new ItemDetailVo();
        BeanUtils.copyProperties(dmItem,dmCinema);
        itemDetailVo.setStartTime(dmItem.getStartTime().toString());
        itemDetailVo.setEndTime(dmItem.getEndTime().toString());
        itemDetailVo.setState(dmItem.getState() + " ");
        itemDetailVo.setImgUrl(dmImageList.get(0).getImgUrl());
        if (EmptyUtils.isEmpty(dmCinema)){
            BeanUtils.copyProperties(dmCinema,itemDetailVo);
        }
        itemDetailVo.setId(dmItem.getId());
        return itemDetailVo;
    }
    @Override
    public List<DmImage> getImageList(Long id, Integer type, Integer category) throws Exception {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("targetId", id);
        param.put("type",type);
        param.put("category",category);
        return restDmImageClient.queryDmImageList(id,type,category);
    }
}
